package com.yuezhi.acl.config;

import com.yuezhi.acl.security.filter.TokenAuthFilter;
import com.yuezhi.acl.security.filter.TokenLoginFilter;
import com.yuezhi.acl.security.handler.MyAuthenticationEntryPoint;
import com.yuezhi.acl.security.handler.MyLogoutHandler;
import com.yuezhi.acl.security.handler.MyPasswordEncoder;
import com.yuezhi.acl.security.handler.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private RedisTemplate redisTemplate;
    private UserDetailsService userDetailsService;
    private TokenManager tokenManager;
    private MyPasswordEncoder myPasswordEncoder;

    @Autowired
    public WebSecurityConfig(RedisTemplate redisTemplate, UserDetailsService userDetailsService,
                             TokenManager tokenManager, MyPasswordEncoder myPasswordEncoder) {

        this.redisTemplate = redisTemplate;
        this.userDetailsService = userDetailsService;
        this.tokenManager = tokenManager;
        this.myPasswordEncoder = myPasswordEncoder;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
        //调用userDetailsService和密码处理
        auth.userDetailsService(userDetailsService).passwordEncoder(myPasswordEncoder);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
        //不进行认证的路径，可以直接访问
        web.ignoring().antMatchers("/api/**","/admin/acl/index/captcha");
    }

    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    //设置退出的地址和token，redis操作地址
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new MyAuthenticationEntryPoint())//没有权限访问
                .and().csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().logout().logoutUrl("/admin/acl/index/logout")//退出路径
                .addLogoutHandler(new MyLogoutHandler(tokenManager,redisTemplate)).and()
                .addFilter(new TokenLoginFilter(authenticationManager(), tokenManager, redisTemplate))
                .addFilter(new TokenAuthFilter(authenticationManager(), tokenManager, redisTemplate)).httpBasic();
    }
}
